package com.shxc.smarttrade.old.modal;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.shxc.smarttrade.old.bean.MasterCopyInfo;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;

import org.xchris.x;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/9/28.
 */

public class MasterDetailModal implements IMasterDetailActivity {
    @Override
    public void getMasterCopyInfo(String accessToken, int usweId, String userCode, final IRequestServiceListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("userId", String.valueOf(usweId));
        map.put("userCode", userCode);
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.MASTER_COPY_INFO, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");
            }

            @Override
            public void success(String response, int id) {
                if (response == null || new JsonParser().parse(response).isJsonNull()) {
                    listener.faild("数据异常");
                    return;
                }
                Gson gson = new Gson();
                MasterCopyInfo masterCopyInfo = gson.fromJson(response, MasterCopyInfo.class);
                if (masterCopyInfo.getErr_code().equals("0"))
                    listener.success(masterCopyInfo.getData().getCopyInfo());
                else
                    listener.faild(masterCopyInfo.getErr_msg());
            }
        });
    }

    @Override
    public void copyMaster(String accessToken, int userId, String userCode, String amt, final int tag, final IRequestServiceListener listener) {

        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("userId", String.valueOf(userId));
        map.put("userCode", userCode);
        map.put("amt", amt);
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.MASTER_COPY, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");
            }

            @Override
            public void success(String response, int id) {
                if (response == null || new JsonParser().parse(response).isJsonNull()) {
                    listener.faild("数据异常");
                    return;
                }
                JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                String errCode = "";
                String errMsg = "";
                if (!jsonObject.get("err_code").isJsonNull())
                    errCode = jsonObject.get("err_code").getAsString();
                if (!jsonObject.get("err_msg").isJsonNull())
                    errMsg = jsonObject.get("err_msg").getAsString();
                if (errCode.equals("0")) {
                    if (tag == 0)
                        listener.success("跟单成功");
                    else if (tag == 1)
                        listener.success("修改成功");

                } else
                    listener.faild(errMsg);

            }
        });
    }

    @Override
    public void cancelMasterCopy(String accessToken, int userId, String userCode, int isClose, final IRequestServiceListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("userId", String.valueOf(userId));
        map.put("userCode", userCode);
        map.put("isClose", String.valueOf(isClose));
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.MASTER_COPY_CANCLE, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");
            }

            @Override
            public void success(String response, int id) {
                if (response == null || new JsonParser().parse(response).isJsonNull()) {
                    listener.faild("数据异常");
                    return;
                }
                JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                String errCode = "";
                String errMsg = "";
                if (!jsonObject.get("err_code").isJsonNull())
                    errCode = jsonObject.get("err_code").getAsString();
                if (!jsonObject.get("err_msg").isJsonNull())
                    errMsg = jsonObject.get("err_msg").getAsString();
                if (errCode.equals("0"))
                    listener.success("取消成功");
                else
                    listener.faild(errMsg);
            }
        });
    }
}
